<?php
namespace app\task\controller;
use app\task\logic\DealCpBranchLogic;

/**
 * 处理社区巡店分部
 * Created by PhpStorm.
 * User: tyll
 * Date: 2019/09/10
 * Time: 09:20
 */
class DealCpBranch extends Base {

    public function __construct(){
        parent::__construct();
        $this->dcb_logic = new DealCpBranchLogic();
    }

    /**
     * 处理社区巡店分部
     * @return \think\response\Json
     */
    public function deal_cp_branch(){
        $now_date = date('Y-m-d');

        $h = new \app\common\controller\Holiday($now_date);
        $rh = $h->get_date_holiday($now_date);
        if($now_date != $rh['now_date']){
            return json($this->dcb_logic->get_response(1,'生成社区巡店分部失败，原因：当天节假日信息不对'));
        }
        $holiday = $rh['holiday'];

        $maps = [];
        $maps[] = ['key','=','cp_time'];
        $conf_info = $this->dcb_logic->get_conf_info($maps);
        $cp_time = json_decode($conf_info['cp_time'],true);
        $code_arr = array_column($cp_time,'code');
        array_push($code_arr,'other');

        $maps = [];
        $branchids = model('branch')->get_column($maps,'id');
        foreach($branchids as $k_1 => $v_1){
            foreach($code_arr as $k_2 => $v_2){
                $maps = [];
                $maps[] = ['branchid','=',$v_1];
                $maps[] = ['time_code','=',$v_2];
                $maps[] = ['now_date','=',$now_date];
                $cb_detail = model('cp_branch')->get_detail($maps,'id');
                if(empty($cb_detail)){
                    $cb_data = [];
                    $cb_data['branchid'] = $v_1;
                    $cb_data['time_code'] = $v_2;
                    $cb_data['now_date'] = $now_date;
                    $cb_data['holiday'] = $holiday;
                    $r = model('cp_branch')->insert($cb_data);
                }
            }
        }
        return json($this->dcb_logic->get_response(1,'生成社区巡店分部，日期：' . $now_date));
    }
}
